package binaryTree;

import java.util.ArrayList;
import java.util.List;

public class Class257 {

    public List<String> binaryTreePaths(TreeNode root) {
        ArrayList<String> res=  new  ArrayList<>();
        if(root==null){
            return res;
        }
        ArrayList<Integer>  paths=new ArrayList<>();
        traveral(root,paths,res);
        return  res;

    }

    public void  traveral(TreeNode  root,ArrayList  paths,ArrayList  res){
        paths.add(root.val);

        if(root.left==null&&root.right==null){
            StringBuffer  sb=new  StringBuffer();
            for(int i=0;i<paths.size()-1;i++){
                sb.append(paths.get(i)).append("->");
            }
            sb.append(paths.get(paths.size()-1));
            res.add(sb.toString());
            return;
        }

        if(root.left!=null){
            traveral(root.left,paths,res);
            paths.remove(paths.size()-1);
        }

        if(root.right!=null){
            traveral(root.right,paths,res);
            paths.remove(paths.size()-1);
        }

    }
}
